<?php
$tixian_state=array("<font color=#ff3300>等待审核</font>","<font color=#009900>提现成功</font>","<font color=#333333>提现失败</font>");

$category=array(
				1=>"综合百货",
				"图书/音像",
				"礼品/鲜花",
				"交友/婚介/聊天",
				"鲜花速递/蛋糕配送/园艺花艺",
				"化妆品/美容",
				"教育/培训",
				"游戏/小游戏",
				"机票/酒店",
				"IT硬件/软件/服务",
				"办公用品",
				"健身/体育/运动",
				"成人用品",
				"金融/投资/理财",
				"电视购物",
				"DIY娱乐/休闲",
				"母婴/幼儿",
				"服装/服饰",
				"医药/保健",
				"餐饮/饮料",
				"手机/数码",
				"家居/电器",
				"女性/内衣",
				"箱包/鞋类/配饰",
				"汽车用品",
				"其他",
				"门户/综合",
				"珠宝首饰/",
				"网络调查",
				"会议/展览/活动",
				);
//下拉表单 $array:数组  $name:select的name和id  $id:被选中选项的id  $interval:颜色间隔
function select($array,$name,$id=10000,$interval=5){
	echo "<select name='$name' id='$name'>";
	foreach($array as $key=>$val){
		if($id==$key) $select='selected';
		else $select='';
		if($key!=0){
			if((floor(($key-1)/$interval))%2!=0) $bg="#dfefd7";
		    else $bg="";
			}
		echo "<option value='$key' $select style='background:$bg'>$val</option>";
		}
	echo "</select>";
	}

function sql_sel($table,$sel_field,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;
}

function sql_sel_arr($table,$sel_field,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$arr=array();
	$sql="select $sel_field from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			$arr[]=$row;
		}
	}
	return $arr;
}

function one2two($arr){//一维数组转二维数组
    if(count($arr, 1) == count($arr)){
	    $arr=array($arr);
    }
    return $arr;
}

function sql_count($table,$count_field,$wh_field,$wh_value,$and=''){//and为自定义的筛选条件 例：$and='and id=1'
	global $BIAOTOU;
	$sql="select count($count_field) from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query = mysql_query($sql);
	$row=mysql_fetch_array($query);
	return $row[0]?$row[0]:0;
}

function sql_sum($table,$count_field,$wh_field,$wh_value,$and=''){//and为自定义的筛选条件 例：$and='and id=1'
	global $BIAOTOU;
	$sql="select sum($count_field) from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query = mysql_query($sql);
	$row=mysql_fetch_array($query);
	return $row[0]?$row[0]:0;
}

function sql_update_one($table, $up_field, $up_value, $wh_field, $wh_value, $xs = 0) { //更新某个字段的值，$xs表示更新的方式，0表示累加，1表示更新
	global $BIAOTOU;
	if ($xs == 0){
		$sql = "update " . $BIAOTOU . "$table set $up_field=$up_field+'$up_value' where $wh_field='$wh_value'";
	}
	if ($xs == 1){
		$sql = "update " . $BIAOTOU . "$table set $up_field='$up_value' where $wh_field='$wh_value'";
	}
	$query = mysql_query($sql);
	if ($query){
		return sql_sel($table, $up_field, $wh_field, $wh_value);
	}
	else{
		return false;
	}
}
	
function sql_insert_one($table,$filed_arr){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($filed_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	$query=mysql_query($sql);
	if($query) return 1;
	else return mysql_error();
}

function sql_update($table,$filed_arr,$wh_field,$wh_value){
    global $BIAOTOU;
	$sql="";
	foreach($filed_arr as $k=>$v){
		$sql="`$k`='$v',".$sql;
		}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where `$wh_field`=$wh_value;";
	mysql_query($sql);
}

function sql_delete($table,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$sql="delete from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	mysql_query($sql);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function re_login($name,$pwd,$dduserid,$savelogin=""){
    $_SESSION["ddusername"] = $name;
    $_SESSION["dduserid"] = $dduserid;
	if(strlen($pwd)!=32){
	    $pwd=md5($pwd);
	}
	if($savelogin){
	    setcookie("ddusername", $name, time() + 1000 * 24 * 60 * 60);
        setcookie("ddpassword", $pwd, time() + 1000 * 24 * 60 * 60);
		setcookie("dduserid", $dduserid, time() + 1000 * 24 * 60 * 60);
	}  
}

function inc_fanli($dduserid,$fxje,$tgje,$order_code){
    sql_update_one("user", "money", $fxje, "Id", $dduserid, 0);//增加会员金额
	$ddusername=sql_sel_one("user", "ddusername", "Id", $dduserid);//求会员名
	
	//用户消息
	$title='您获得了新的交易返现！';
	$content="交易号为：".$order_code."的交易确认完毕，您获得了 ".$fxje." 元的返现！";
	$filed_arr=array('title'=>$title,'content'=>$content,'addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$ddusername);
	sql_insert_one("msg",$filed_arr);
	
	//用户明细
	$shijian="交易返现";
	$memo="交易号$order_code";
	$filed_arr=array('ddusername'=>$ddusername,'shijian'=>$shijian,'addtime'=>date('Y-m-d H:i:s'),'je'=>$fxje,'memo'=>$memo);
	sql_insert_one("mingxi",$filed_arr);
	
	//求推荐人
	$tjrid=sql_sel_one("user", "tjr", "ddusername", $ddusername);
	if($tjrid>0){
		$tjrname=sql_sel_one("user", "ddusername", "Id", $tjrid);
	    //增加推荐人佣金
		sql_update_one("user", "money", $tgje, "Id", $tjrid, 0);
					
		//用户消息
		$title='您获得了新的推广佣金！';
		$content="您推荐的会员" . $ddusername . "，完成一笔交易，交易号为：" . $order_code . "确认完毕，您获得了 " . $tgje . " 元的推广佣金！";
		$filed_arr=array('title'=>$title,'content'=>$content,'addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$tjrname);
		sql_insert_one("msg",$filed_arr);
					
		//用户明细
		$shijian="推广佣金";
		$memo="交易人$ddusername";
		$filed_arr=array('ddusername'=>$tjrname,'shijian'=>$shijian,'addtime'=>date('Y-m-d H:i:s'),'je'=>$tgje,'memo'=>$memo);
		sql_insert_one("mingxi",$filed_arr);
	}
}

function level($dengji) {
	if ($dengji < 10) {
		$level = 1;
	}
	elseif ($dengji >= 10 && $dengji < 30) {
		$level = 2;
	}
	elseif ($dengji >= 30 && $dengji < 60) {
		$level = 3;
	}
	elseif ($dengji >= 60 && $dengji < 100) {
		$level = 4;
	}
	elseif ($dengji >= 100 && $dengji < 150) {
		$level = 5;
	}
	elseif ($dengji >= 150 && $dengji < 210) {
		$level = 6;
	}
	elseif ($dengji >= 210 && $dengji < 280) {
		$level = 7;
	}
	elseif ($dengji >= 280 && $dengji < 360) {
		$level = 8;
	}
	elseif ($dengji >= 360 && $dengji < 450) {
		$level = 9;
	}
	elseif ($dengji >= 450 && $dengji < 550) {
		$level = 10;
	}
	elseif ($dengji >= 550 && $dengji < 660) {
		$level = 11;
	}
	elseif ($dengji >= 660 && $dengji < 780) {
		$level = 12;
	}
	elseif ($dengji >= 780 && $dengji < 910) {
		$level = 13;
	}
	elseif ($dengji >= 910 && $dengji < 1050) {
		$level = 14;
	}
	elseif ($dengji >= 1050 && $dengji < 1200) {
		$level = 15;
	}
	elseif ($dengji >= 1200 && $dengji < 1360) {
		$level = 16;
	}
	elseif ($dengji >= 1360 && $dengji < 1530) {
		$level = 17;
	}
	elseif ($dengji >= 1530 && $dengji < 1710) {
		$level = 18;
	}
	elseif ($dengji >= 1710 && $dengji < 1900) {
		$level = 19;
	} else {
		$level = 20;
	}
	return $level;
}





